<?php
namespace Manage\Controller;
use Think\Controller;


class AdminFyCountCsdtController extends Controller {

    public function index(){
        $this->display();
    }

    public function get_all(){
        $map = array_merge(get_page_query_map(I('get.')), get_page_query_map(I('post.')));
        $page = max(I('get.page'), 1);
        $rows_num = get_kv_val('admin_page_list_num')?get_kv_val('admin_page_list_num'):I('get.rows');
        $list = $this->gen_search($map, $page, $rows_num);
        echo json_encode($list);
    }

    public function mut_bz_same_time(){
        $this->display();
    }

    public function mut_bz(){
        $this->display();
    }

    public function mut_bz_same_time_query(){
        $ps_id = I('post.ps_id');
        $fy_id = I('post.f_id');
        $date_array = I('post.date');
        $start_date = I('post.start_date');
        $end_date = I('post.end_date');
        $start_hour = I('post.start_hour');
        $end_hour = I('post.end_hour');
        $query_type = I('post.query_type');
        if(!$ps_id){
            echo json_encode(array());
            exit;
        }
        if($start_date==''){
            date("Y-m-d");
        }
        if($end_date==''){
            date("Y-m-d");
        }
        if($start_hour==''){
            $start_hour = 0;
        }
        if($end_hour==''){
            $end_hour = 23;
        }
        $x = array();
        $x_text = array();
        $date_array = explode(',', $date_array);
        $station = D('Csdt')->queryObj($ps_id, 'id');
        //查询泵机
        if($fy_id){
            $pumps = D('CsdtPump')->select($fy_id);
        }else{
            $pumps = D('CsdtPump')->queryField('ps_id', $ps_id);
        }
        if($query_type == 1){
            //按小时查询
            for($i=$start_hour;$i<=$end_hour;$i++){
                $x[] = substr('00' . $i, -2);
                $x_text[] = $i.'时';
            }
            $legend_data = array();
            $date_array = $date_array[0];
            $date_array = explode('_', $date_array);
            $dt_start = $start_date;
            $dt_end = $end_date;
            $legend_data[] = $dt_start;
            while ($dt_start<$dt_end){
                $dt_start = date("Y-m-d",strtotime("+1 day ".$dt_start));
                $legend_data[] = $dt_start;
            }

            $flow = 0;
            $series = array();
            $all_flow = 0;
            foreach($legend_data as $every_date){
                $this_serie = array();
                $this_serie['name'] = $every_date;
                $this_serie['type'] = 'line';
                $this_serie['data'] = array();
                $this_serie['label'] = array(
                    'normal' => array(
                        'show' => false,
                        'position' => 'top',
                        'formatter'=> '{c}m³'
                    )
                );
                foreach($x as $hour){
                    foreach($pumps as $pump){
                        $obj = $this->cal_flow($pump, $every_date.' '.$hour.':00:00', $every_date.' '.$hour.':59:59');
                        $flow += $obj['flow'];
                        $all_flow += $obj['flow'];
                    }
                    $this_serie['data'][] = $flow;
                    $flow = 0;
                }
                $series[] = $this_serie;
            }
            $title = $station['ps_name'].'抽排量';
            $option = array(
                'title' => array(
                    'text' => $title
                ),
                'tooltip' => array(
                    'trigger' => 'axis'
                ),
                'legend' => array(
                    'width' =>'600px',
                    'right' => '40px',
                    'data' => $legend_data
                ),
                'grid' => array(
                    'left' => '3%',
                    'right' => '5%',
                    'bottom' => '3%',
                    'containLabel' => true,
                ),
                'toolbox' => array(
                    'feature' => array(
                        'saveAsImage' => array()
                    ),
                ),
                'xAxis' => array(
                    'name' => '小时',
                    'type' => 'category',
                    'boundaryGap' => false,
                    'data' => $x_text,
                ),
                'yAxis' => array(
                    'name' => '抽排量(m³)',
                    'type' => 'value',
                ),
                'series' => $series
            );
            echo json_encode(array('option'=>$option,'text'=>$station['ps_name'].'总抽排量为：'.$all_flow.'m³'));
        }else{
            $dt_start = $start_date;
            $dt_end = $end_date;
            while ($dt_start<=$dt_end){
                $x[] = $dt_start;
                $dt_start = date("Y-m-d",strtotime("+1 day ".$dt_start));
            }
            $hour = array();
            for($i=$start_hour;$i<=$end_hour;$i++){
                $hour[] = substr('00' . $i, -2);
                $legend_data[] = $i.'时';
            }
            $flow = 0;
            $series = array();
            $all_flow = 0;
            foreach($hour as $k => $every_hour){
                $this_serie = array();
                $this_serie['name'] = $legend_data[$k];
                $this_serie['type'] = 'line';
                $this_serie['data'] = array();
                foreach($x as $every_date){
                    foreach($pumps as $pump){
//                        dump($every_date.' '.$every_hour.':00:00'.'-----'.$every_date.' '.$every_hour.':59:59');
                        $obj = $this->cal_flow($pump, $every_date.' '.$every_hour.':00:00', $every_date.' '.$every_hour.':59:59');
                        $flow += $obj['flow'];
                        $all_flow += $obj['flow'];
                    }
                    $this_serie['data'][] = $flow;
                    $flow = 0;
                }
                $series[] = $this_serie;
            }
            $title = $station['ps_name'].'抽排量';
            $option = array(
                'title' => array(
                    'text' => $title
                ),
                'tooltip' => array(
                    'trigger' => 'axis'
                ),
                'legend' => array(
                    'width' =>'600px',
                    'right' => '40px',
                    'data' => $legend_data
                ),
                'grid' => array(
                    'left' => '3%',
                    'right' => '5%',
                    'bottom' => '3%',
                    'containLabel' => true,
                ),
                'toolbox' => array(
                    'feature' => array(
                        'saveAsImage' => array()
                    ),
                ),
                'xAxis' => array(
                    'name' => '日期',
                    'type' => 'category',
                    'boundaryGap' => false,
                    'data' => $x,
                ),
                'yAxis' => array(
                    'name' => '抽排量(m³)',
                    'type' => 'value',
                ),
                'series' => $series
            );
            echo json_encode(array('option'=>$option,'text'=>$station['ps_name'].'总抽排量为：'.$all_flow.'m³'));
        }

    }

    public function mut_bz_query(){
        $ps_ids = I('post.ps_id');
        $fy_id = I('post.f_id');
        $start_date = I('post.start_date');
        $end_date = I('post.end_date');
        if(!$ps_ids){
            echo json_encode(array());
            exit;
        }
        if($start_date==''){
            date("Y-m-d");
        }
        if($end_date==''){
            date("Y-m-d");
        }
        $x = array();
        $x_text = array();
        $ps_ids = array_filter(explode(',', $ps_ids));
        $fy_ids = array_filter(explode(',', $fy_id));
        //计算出折线数量
        $legend_ps_ids = array();
        $legend_data = array();
        foreach($ps_ids as $ps_id){
            $station = D('Csdt')->queryObj($ps_id, 'id');
            $legend_ps_ids[] = $station['id'];
            $legend_data[] = $station['ps_name'];
        }
        if($fy_id){
            $temp_pumps = D('CsdtPump')->select($fy_id);
        }
        $have_fy_stations = array();
        foreach($temp_pumps as $a){
            $have_fy_stations[] = $a['ps_id'];
        }
        $dt_start = $start_date;
        $dt_end = $end_date;
        $x[] = $dt_start;
        while ($dt_start<$dt_end){
            $dt_start = date("Y-m-d",strtotime("+1 day ".$dt_start));
            $x[] = $dt_start;
        }
        $flow = 0;
        $series = array();
        $all_flow = 0;
        $text = '';
        foreach($legend_ps_ids as $k => $ps_id){
            $this_serie = array();
            $this_serie['name'] = $legend_data[$k];
            $this_serie['type'] = 'line';
            $this_serie['data'] = array();
            $pumps = D('CsdtPump')->queryField('ps_id', $ps_id);
            if(!empty($have_fy_stations)){
                if(in_array($ps_id , $have_fy_stations)){
                    //如果选择了泵机的调蓄池，就计算泵机，如果没有就提取全部泵机
                    $temp_pumps = array();
                    foreach($pumps as $a){
                        if(in_array($a['id'], $fy_ids)){
                            $temp_pumps[] = $a;
                        }
                    }
                    $pumps = $temp_pumps;
                }
            }
            foreach($x as $every_date){
                foreach($pumps as $pump){
                    $obj = $this->cal_flow($pump, $every_date.' 00:00:00', $every_date.' 23:59:59');
                    $flow += $obj['flow'];
                    $all_flow += $obj['flow'];
                }
                $this_serie['data'][] = $flow;
                $flow = 0;
            }
            $text .= $legend_data[$k].'总抽排量：'.$all_flow.'m³';
            $all_flow = 0;
            $series[] = $this_serie;
        }
        $title = '抽排量对比';
        $option = array(
            'title' => array(
                'text' => $title
            ),
            'tooltip' => array(
                'trigger' => 'axis'
            ),
            'legend' => array(
                'width' =>'600px',
                'right' => '40px',
                'data' => $legend_data
            ),
            'grid' => array(
                'left' => '3%',
                'right' => '5%',
                'bottom' => '3%',
                'containLabel' => true,
            ),
            'toolbox' => array(
                'feature' => array(
                    'saveAsImage' => array()
                ),
            ),
            'xAxis' => array(
                'name' => '日期',
                'type' => 'category',
                'boundaryGap' => false,
                'data' => $x,
            ),
            'yAxis' => array(
                'name' => '抽排量(m³)',
                'type' => 'value',
            ),
            'series' => $series
        );
        echo json_encode(array('option'=>$option, 'text'=>$text));
    }

    public function mut_bz_query_hour(){
        $ps_ids = I('post.ps_id');
        $fy_id = I('post.f_id');
        $start_date = I('post.start_date');
        $end_date = I('post.end_date');
        if(!$ps_ids){
            echo json_encode(array());
            exit;
        }
        if($start_date==''){
            date("Y-m-d");
        }
        if($end_date==''){
            date("Y-m-d");
        }
        $x = array();
        $x_text = array();
        $ps_ids = array_filter(explode(',', $ps_ids));
        $fy_ids = array_filter(explode(',', $fy_id));
        //计算出折线数量
        $legend_ps_ids = array();
        $legend_data = array();
        foreach($ps_ids as $ps_id){
            $station = D('Csdt')->queryObj($ps_id, 'id');
            $legend_ps_ids[] = $station['id'];
            $legend_data[] = $station['ps_name'];
        }
        if($fy_id){
            $temp_pumps = D('CsdtPump')->select($fy_id);
        }
        $have_fy_stations = array();
        foreach($temp_pumps as $a){
            $have_fy_stations[] = $a['ps_id'];
        }
        $dt_start = $start_date;
        $dt_end = $end_date;
        $date_length = 1;
        $date_array[] = $dt_start;
        while ($dt_start<$dt_end){
            $dt_start = date("Y-m-d",strtotime("+1 day ".$dt_start));
            $date_array[] = $dt_start;
            $date_length++;
        }
        $x_index = 0;
        for($i=1;$i<=$date_length;$i++){
            for($j=0;$j<24;$j++){
                $x[] = $x_index;
                $x_text[] = $x_index.'时';
                $x_index++;
            }
        }
        $flow = 0;
        $series = array();
        $text = '';
        $all_flow = 0;
        foreach($legend_ps_ids as $k => $ps_id){
            $this_serie = array();
            $this_serie['name'] = $legend_data[$k];
            $this_serie['type'] = 'line';
            $this_serie['data'] = array();
            $pumps = D('CsdtPump')->queryField('ps_id', $ps_id);
            if(!empty($have_fy_stations)){
                if(in_array($ps_id , $have_fy_stations)){
                    //如果选择了泵机的调蓄池，就计算泵机，如果没有就提取全部泵机
                    $temp_pumps = array();
                    foreach($pumps as $a){
                        if(in_array($a['id'], $fy_ids)){
                            $temp_pumps[] = $a;
                        }
                    }
                    $pumps = $temp_pumps;
                }
            }
            foreach($date_array as $every_date){
                for($i=0;$i<24;$i++){
                    $hour = substr('00' . $i, -2);
                    foreach($pumps as $pump){
                        $obj = $this->cal_flow($pump, $every_date.' '.$hour.':00:00', $every_date.' '.$hour.':59:59');
                        $flow += $obj['flow'];
                        $all_flow +=$obj['flow'];
                    }
                    $this_serie['data'][] = $flow;
                    $flow = 0;
                }
            }
            $text .= $legend_data[$k].'总抽排量：'.$all_flow.'m³';
            $all_flow = 0;
            $series[] = $this_serie;
        }
        $title = '抽排量对比';
        $option = array(
            'title' => array(
                'text' => $title
            ),
            'tooltip' => array(
                'trigger' => 'axis'
            ),
            'legend' => array(
                'width' =>'600px',
                'right' => '40px',
                'data' => $legend_data
            ),
            'grid' => array(
                'left' => '3%',
                'right' => '5%',
                'bottom' => '3%',
                'containLabel' => true,
            ),
            'toolbox' => array(
                'feature' => array(
                    'saveAsImage' => array()
                ),
            ),
            'xAxis' => array(
                'name' => '日期',
                'type' => 'category',
                'boundaryGap' => false,
                'data' => $x_text,
            ),
            'yAxis' => array(
                'name' => '抽排量(m³)',
                'type' => 'value',
            ),
            'series' => $series
        );
        echo json_encode(array('option'=>$option, 'text'=>$text));
    }

    private function gen_search($map, $page, $rows_num){
        $pump_pump_model = D('CsdtPump');
        $station_model = D('Csdt');
        $record_model = D('CsdtPumpRecord');
        $station_ids = null;
        $status = '';
        $p_type = '';
        $f_id = '';
        $start = '';
        $end = '';
        $now_datetime = '';
        if(isset($map['now_datetime'])){
            $now_datetime = str_replace('%', '', $map['now_datetime'][1]);
            unset($map['now_datetime']);
        }
        if(isset($map['ps_id'])){
            $station_ids = my_array_filter_to_text(str_replace('%', '', $map['ps_id'][1]), ',');
            unset($map['ps_id']);
        }
        if(isset($map['f_id'])){
            $f_id = str_replace('%', '', $map['f_id'][1]);
            $f_id = array_filter(explode(',', $f_id));
            unset($map['f_id']);
        }
        if(isset($map['status'])){
            $status = str_replace('%', '', $map['status'][1]);
            unset($map['status']);
        }
        if(isset($map['p_type'])){
            $p_type = str_replace('%', '', $map['p_type'][1]);
            unset($map['p_type']);
        }
        if($map['start']&&$map['end']){
            $start = str_replace('%', '', $map['start'][1]);
            $end = str_replace('%', '', $map['end'][1]);
            unset($map['start']);
            unset($map['end']);
        }
        if($station_ids){
            $stations = $station_model->order('ps_name desc,mobile desc')->select($station_ids);
        }else{
            $stations = $station_model->order('ps_name desc,mobile desc')->select();
        }
        $fys = array();
        //查询出所有设备
        foreach($stations as $station){
            $query_map['ps_id'] = array('eq', $station['id']);
            if($p_type){
                $query_map['p_type'] = array('eq', $p_type);
            }
            $fys = array_merge($fys, $pump_pump_model->where($query_map)->order('p_number asc')->select());
        }
        $result = array();
        $all_fys = array();
        $error_list = array();
        foreach($fys as $k => $fy){
            if(!in_array($fy['id'], $f_id)&&!empty($f_id))
                continue;
            $fy_status =  $record_model->get_last_one($fy['id']);
            if($fy_status == 999 || $fy_status == -1)
                $fy_status = 0;
            if($status == '' || $fy_status == $status){
                $fys[$k]['status'] = $fy_status;
                if($fy_status == -2)
                    $error_list[] = $fy;
                else
                    $all_fys[] = $fy;
            }
        }

        $result = array_merge($error_list, $all_fys);
        $all_fys = array_slice($result, ($page-1)*$rows_num, $rows_num);
        $yestoday = date("Y-m-d",strtotime("-1 day ".date('Y-m-d')));
        $ps_id = 0;
        $bz_t_f = 0;
        $bz_q_f = 0;
        $query_bj_flow = 0;
        foreach($all_fys as $k => $fy){
            $last_record = $record_model->get_last_one_record($fy['id']);
            $fy_status =  $last_record['status'];
            if($fy_status == 999 || $fy_status == -1 || $fy_status == null)
                $fy_status = 0;
            if($status == '' || $fy_status == $status){
                $sta_temp = $station_model->queryObj($fy['ps_id'], 'id');
                $all_fys[$k]['sta_name'] = $sta_temp['ps_name'];
                $all_fys[$k]['sta_id'] = $sta_temp['id'];
                $all_fys[$k]['status_text'] = device_status_option($fy_status, false);
                $all_fys[$k]['p_type'] = pump_pump_type_option($fy['p_type'], false);
                $all_fys[$k]['status'] = $fy_status;
                //今日抽排
                $today = $this->cal_flow($fy, date('Y-m-d').' 00:00:00', $now_datetime);
                $all_fys[$k]['today_flow'] = $today['flow'];
                //昨日抽排
                if(S('yestoday_'.$fy['ps_id'].'_'.$start.'_'.$end)){
                }else{
                    $yestoday_a = $this->cal_flow_by_bz($fy['ps_id'], -1, $yestoday.' 00:00:00', $yestoday.' 23:59:59');
                    S('yestoday_'.$fy['ps_id'].'_'.$start.'_'.$end, $yestoday_a);
                }
                $all_fys[$k]['yes_flow'] = S('yestoday_'.$fy['ps_id'].'_'.$start.'_'.$end);

                //总抽排量
                if($ps_id != $fy['ps_id']){
                    $bz_t_f = $this->cal_flow_by_bz($fy['ps_id'], -1, date('Y-m-d').' 00:00:00', $now_datetime);
                }
                //查询抽排
                if($start!=''&&$end!=''){
                    if($ps_id != $fy['ps_id']){
                        $bz_q_f = $this->cal_flow_by_bz($fy['ps_id'], -1, $start, $end);
                    }
                    $bj_query = $this->cal_flow($fy, $start, $end);
                    $all_fys[$k]['query_flow_bj'] = $bj_query['flow'];
                    $all_fys[$k]['query_flow'] = $bz_q_f;
                }
                $all_fys[$k]['total_flow'] = $bz_t_f;

            }
        }
        return array('total'=>count($result), 'rows'=>$all_fys);
    }

    public function get_text_query(){
        $now_datetime = I('post.now_datetime')?I('post.now_datetime'):date('Y-m-d H:i:s');
        $station_model = D('Csdt');
        $yestoday = date("Y-m-d",strtotime("-1 day ".date('Y-m-d')));
        $yestoday_flow = 0;
        if(I('post.ps_id')){
            $q_ps_ids = array_filter(explode(',', I('post.ps_id')));
        }
        $all_ps_ids = $station_model->field('id')->select();
        if(!S('csdt_'.$yestoday.'_text')){
            foreach($all_ps_ids as $ps){
                $yestoday_flow += $this->cal_flow_by_bz($ps['id'], -1, $yestoday.' 00:00:00', $yestoday.' 23:59:59');
            }
            S('csdt_'.$yestoday.'_text', $yestoday_flow, 36000);
        }
        $yestoday_flow = S('csdt_'.$yestoday.'_text');
        $today_flow = 0;
        foreach($all_ps_ids as $ps){
            $today_flow += $this->cal_flow_by_bz($ps['id'], -1, date('Y-m-d').' 00:00:00', $now_datetime);
        }

        $today_ys_flow = 0;
        foreach($all_ps_ids as $ps){
            $today_ys_flow += $this->cal_flow_by_bz($ps['id'], 1, date('Y-m-d').' 00:00:00', $now_datetime);
        }

        $today_ws_flow = 0;
        foreach($all_ps_ids as $ps){
            $today_ws_flow += $this->cal_flow_by_bz($ps['id'], 2, date('Y-m-d').' 00:00:00', $now_datetime);
        }

        $query_flow = 0;
        $query_ys_flow = 0;
        $queryws_flow = 0;
        if(I('post.start')&&I('post.end')){
            //如果没有选择设备
            if(!I('post.f_id')){
                foreach($q_ps_ids as $ps){
                    $query_flow += $this->cal_flow_by_bz($ps, -1, I('post.start'), I('post.end'));
                }
                foreach($q_ps_ids as $ps){
                    $query_ys_flow += $this->cal_flow_by_bz($ps, 1, I('post.start'), I('post.end'));
                }
                foreach($q_ps_ids as $ps){
                    $queryws_flow += $this->cal_flow_by_bz($ps, 2, I('post.start'), I('post.end'));
                }
            }else{
                //如果选择了设备
                $fy_model = D('CsdtPump');
                $fy_ids = array_filter(explode(',', I('post.f_id')));
                foreach($fy_ids as $fy_id){
                    $map['id'] = array('eq', $fy_id);
                    $fy = $fy_model->where($map)->find();
                    $temp = $this->cal_flow($fy, I('post.start'), I('post.end'));
                    $query_flow += $temp['flow'];
                    if($fy['p_type'] == 1){
                        //雨水
                        $query_ys_flow += $temp['flow'];
                    }else if($fy['p_type'] == 2){
                        $queryws_flow += $temp['flow'];
                    }
                }
                unset($fy_model);
            }
            unset($station_model);
        }
        echo json_encode(array('yestoday_flow'=>number_format($yestoday_flow), 'today_flow'=>number_format($today_flow), 'today_ys_flow'=>number_format($today_ys_flow),
            'today_ws_flow'=>number_format($today_ws_flow), 'query_flow'=>number_format($query_flow), 'query_ys_flow'=>number_format($query_ys_flow), 'queryws_flow'=>number_format($queryws_flow)));
    }

    public function query_today_bz_charts_line(){
        $x = array();
        $x_text = array();
        for($i=0;$i<24;$i++){
            $x[] = substr('00' . $i, -2);
            $x_text[] = $i.'时';
        }
        //查询根据时段查询并计算流量
        $all = array();
        $data_min = array();
        $this_day = date('Y-m-d');
        foreach($x as $hour){
            if($hour >= intval(date('H'))){
                $all[] = 0;
            }else{
                $time_start = $this_day.' '.$hour.':00:00';
                $time_end = $this_day.' '.$hour.':59:59';
                $temp_flow = $this->cal_flow_by_log($time_start, $time_end);
                $all[] = $temp_flow;
                $data_min[] = $temp_flow;
            }
        }
        $title = date('Y年m月d日').'调蓄池抽排量';
        $option = array(
            'title' => array(
                'text' => $title
            ),
            'tooltip' => array(
                'trigger' => 'axis'
            ),
            'legend' => array(
                'data' => array($title)
            ),
            'grid' => array(
                'left' => '3%',
                'right' => '5%',
                'bottom' => '3%',
                'containLabel' => true,
            ),
            'toolbox' => array(
                'feature' => array(
                    'saveAsImage' => array()
                ),
            ),
            'xAxis' => array(
                'name' => '小时',
                'type' => 'category',
                'boundaryGap' => false,
                'data' => $x_text,
            ),
            'yAxis' => array(
                'name' => '抽排量(m³)',
                'type' => 'value',
                'max ' => max($data_min)+5000,
                'splitNumber' => 3,
            ),
            'series' => array(
                array(
                    'name' => '调蓄池抽排量',
                    'type' => 'line',
                    'data' => $all
                ),
            ),
        );
        echo json_encode($option);
    }

    /*******************************************************************************************************************/

    private function cal_flow($pump, $start, $end){
        if($start >= date('Y-m-d H:i:s')){
            return array('time' => 0, 'flow'=>0);
        }
        if($end > date('Y-m-d H:i:s')){
            $end = date('Y-m-d H:i:s');
        }
        $record_model = D('CsdtPumpRecord');
//        $str = '(csdt_start>=\''.$start.'\' or (csdt_off>=\''.$start.'\' and csdt_off<=\''.$end.'\')) and date>=\''.$start.'\' and date<=\''.$end.'\'';
        //$str = 'csdt_start<=\''.$end.'\' and (csdt_off>=\''.$start.'\' or csdt_off is null)';
        $str = '((csdt_start >= \''.$start.'\' AND csdt_start <= \''.$end.'\') OR
        (csdt_start <= \''.$start.'\' AND csdt_off >= \''.$end.'\') OR
        (csdt_off >= \''.$start.'\' AND csdt_off <= \''.$end.'\')
        OR (csdt_off is null AND csdt_start <=  \''.$end.'\'))';
        $map['csdtpumpid'] = array('eq', $pump['id']);
        $map['status'] = array('egt', 0);
        $logs = $record_model->where($map)->where($str)->select();
//        dump($record_model->getLastSql());
        $temp_start = '';
        $temp_end = '';
        $all_time = 0;
//        dump($start.'----'.$end);
        foreach($logs as $log){
            if($log['csdt_start']>$start)
                $temp_start = $log['csdt_start'];
            else
                $temp_start = $start;
            if($log['csdt_off']>$end || !$log['csdt_off'])
                $temp_end = $end;
            else
                $temp_end = $log['csdt_off'];
            $time = strtotime($temp_end)-strtotime($temp_start);
//            dump($temp_start.'----'.$temp_end);
//            dump($time);
            $this_time = round(timestamp_to_time('hour', $time), 2);
            $all_time += $this_time;
        }
        return array('time' => $all_time, 'flow'=>round($all_time*$pump['p_flow_meas']));
    }

    private function cal_flow_by_log($start, $end){
//        $record_model = M('CsdtPumpRecord');
//        $str = 'csdt_start<=\''.$end.'\' and (csdt_off>=\''.$start.'\' or csdt_off is null)';
////        dump($str);
//        $map['status'] = array('neq', -2);
//        $logs = $record_model->where($map)->where($str)->select();
//        $temp_start = '';
//        $temp_end = '';
//        $flow = 0;
//        foreach($logs as $log){
//            if($log['csdt_start']>$start)
//                $temp_start = $log['csdt_start'];
//            else
//                $temp_start = $start;
//            if($log['csdt_off']>$end || !$log['csdt_off'])
//                $temp_end = $end;
//            else
//                $temp_end = $log['csdt_off'];
//            $time = strtotime($temp_end)-strtotime($temp_start);
////            dump($temp_start.'----'.$temp_end);
////            dump($time);
//            $this_time = round(timestamp_to_time('hour', $time), 2);
//            $this_pump = $this->get_csdt_pumps($log['CsdtPumpid']);
//            $flow += $this_time*$this_pump['p_flow_meas'];
//        }
        if($start >= date('Y-m-d H:i:s')){
            return 0;
        }
        if($end > date('Y-m-d H:i:s')){
            $end = date('Y-m-d H:i:s');
        }
        $flow = $this->query_line_hour_cache($start, $end);
        return $flow;
    }

    private function cal_bz_flow($fys, $cal_field, $to_field){
        $ps_ids = array();
        $flows = array();
        foreach($fys as $fy){
            if(!in_array($fy['ps_id'], $ps_ids)){
                $ps_ids[] = $fy['ps_id'];
            }
        }
        $temp_flow = 0;
        foreach($ps_ids as $ps_id){
            foreach($fys as $fy){
                if($ps_id == $fy['ps_id']){
                    $temp_flow += $fy[$cal_field];
                }
            }
            $flows[$ps_id] = round($temp_flow);
            $temp_flow = 0;
        }
        foreach($fys as $k => $fy){
            $fys[$k][$to_field] = $flows[$fy['ps_id']];
        }
        return $fys;
    }

    /**
     * @param $ps_id
     * @param $p_type   泵机类型
     * @param $start
     * @param $end
     * @return flow
     * 计算单个调蓄池时间段内的抽排量
     */
    private function cal_flow_by_bz($ps_id, $p_type=-1, $start, $end){
        if($start >= date('Y-m-d H:i:s')){
            return 0;
        }
        if($end > date('Y-m-d H:i:s')){
            $end = date('Y-m-d H:i:s');
        }
        $pump_pump_model = D('CsdtPump');
        //查询出所有设备
        $query_map['ps_id'] = array('eq', $ps_id);
        if($p_type>0){
            $query_map['p_type'] = array('eq', $p_type);
        }
        $fys = $pump_pump_model->where($query_map)->select();
        $flow_t = 0;
        foreach($fys as $k => $fy){
            //今日抽排
            $flow = $this->cal_flow($fy, $start, $end);
            $flow_t += $flow['flow'];
        }
        return $flow_t;
    }

    private function get_csdt_pumps($id){
        if(!S('csdt_pumps')){
            $array = array();
            $temps = D('CsdtPump')->select();
            foreach($temps as $temp){
                $array[$temp['id']] = $temp;
            }
            S('csdt_pumps',$array,300);
        }
        static $pumps;
        if(!isset($pumps)){
            $pumps = S('csdt_pumps');
        }
        $one = $pumps[$id];
        return $one?$one:false;
    }

    private function query_line_hour_cache($start, $end){
        $a=0;
        if($start >= date('Y-m-d H:i:s')){
            return 0;
        }
        if($end > date('Y-m-d H:i:s')){
            $end = date('Y-m-d H:i:s');
        }
        if(!S($start.'_'.$end)){
            $record_model = M('CsdtPumpRecord');
            $str = 'csdt_start<=\''.$end.'\' and (csdt_off>=\''.$start.'\' or csdt_off is null)';
            $map['status'] = array('egt', 0);
            $logs = $record_model->where($map)->where($str)->select();
//            dump($record_model->getLastSql());
            $temp_start = '';
            $temp_end = '';
            $flow = 0;
            foreach($logs as $log){
                if($log['csdt_start']>$start)
                    $temp_start = $log['csdt_start'];
                else
                    $temp_start = $start;
                if($log['csdt_off']>$end || !$log['csdt_off'])
                    $temp_end = $end;
                else
                    $temp_end = $log['csdt_off'];
                $time = strtotime($temp_end)-strtotime($temp_start);
//            dump($temp_start.'----'.$temp_end);
//            dump($time);
                $this_time = round(timestamp_to_time('hour', $time), 2);
                $this_pump = $this->get_csdt_pumps($log['csdtpumpid']);
                $flow += $this_time*$this_pump['p_flow_meas'];
            }
            S('csdt_'.$start.'_'.$end, $flow, 36000);
            $flow = 0;
        }
        $one = S('csdt_'.$start.'_'.$end);
        return $one?$one:false;
    }
}